const axios = require('axios');
const config = require('../../config/index');

/**
 * 验证ticket
 * @param {string} ticket ticket
 * @returns
 */
async function validateTicket(ticket) {
  if (!ticket) {
    return { error: 1, message: 'ticket不合法' };
  }

  const options = {
    method: 'post',
    baseURL: `http://${config.sso.host}:${config.sso.PORT}`,
    url: '/auth/validate',
    params: {
      ticket,
    },
    responseType: 'json',
  };

  return axios(options)
    .then(res => res.data)
    .catch(() => {
      console.error(`验证ticket失败`);
      return { error: 1, message: `验证ticket失败` };
    });
}

module.exports = { validateTicket };
